package com.zyw.customcalendar;

import java.util.ArrayList;
import com.zyw.customcalendar.dao.CalendarSQLiteDao;
import com.zyw.customcalendar.model.EventModel;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class EventListActivity extends Activity{

	private ListView elist;

	private CalendarSQLiteDao sqliteDao;

	private ArrayList<EventModel> eventlist;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_event_list);

		InitData();
		InitView();
		setAdapter();
	}

	public void InitView(){
		elist = (ListView) findViewById(R.id.event_list);
	}

	public void InitData(){
		sqliteDao = new CalendarSQLiteDao();
		eventlist = sqliteDao.getLikeData(getIntent().getStringExtra("startTime"));
		if(eventlist == null)
			eventlist = new ArrayList<>(0);
	}

	class HolderView{
		TextView textContent;
		TextView textStart;
		TextView textChixu;
		TextView textChongfu;
	}

	public void setAdapter(){
		BaseAdapter adapter = new BaseAdapter() {

			@SuppressLint({ "ViewHolder", "InflateParams" })
			@Override
			public View getView(int index, View view, ViewGroup group) {
				// TODO Auto-generated method stub
				HolderView holderView = null;
				if(view == null){
					holderView = new HolderView();
					view = LayoutInflater.from(EventListActivity.this).inflate(
							R.layout.event_list_item_view, null);
					holderView.textContent = (TextView) view.findViewById(R.id.event_content);
					holderView.textStart = (TextView) view.findViewById(R.id.event_start);
					holderView.textChixu = (TextView) view.findViewById(R.id.event_chixu);
					holderView.textChongfu = (TextView) view.findViewById(R.id.event_chongfu);
					view.setTag(holderView);
				}else{
					holderView = (HolderView) view.getTag();
				}
				EventModel model = eventlist.get(index);
				holderView.textContent.setText(model.content);
				holderView.textStart.setText(model.startTime);
				holderView.textChixu.setText(model.chixuIndex);
				holderView.textChongfu.setText(model.chongfuIndex);
				return view;
			}

			@Override
			public long getItemId(int index) {
				// TODO Auto-generated method stub
				return index;
			}

			@Override
			public Object getItem(int index) {
				// TODO Auto-generated method stub
				return eventlist.get(index);
			}

			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return eventlist.size();
			}
		};
		elist.setAdapter(adapter);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		//EventBus.getDefault().unregister(this);
		super.onDestroy();
	}


}
